package com.example.mylearn.juc.example;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SyncAndVolatile {


    private int val = 0;


    public synchronized void incr() {
        val++;
    }


    public static void main(String[] args) {

        SyncAndVolatile sync = new SyncAndVolatile();

//        for (int i = 0; i < 100000; i++) {
//            new Thread(sync::incr).start();
//        }
//        System.out.println(sync.val);

        ExecutorService executorService = Executors.newFixedThreadPool(8);
            for (int i = 0; i < 10; i++) {
                executorService.execute(()->{
                    for (int j = 0; j < 1000; j++) {
                        sync.incr();
                    }
                });
            }

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(sync.val);


    }










}
